﻿using SqlSugar;
using SXICE.Core;
using SXICE.Core.Domains;
using SXICE.Core.Helpers;
using SXICE.Core.Infrastructure;
using SXICE.Core.Models;
using SXICE.Core.Models.DTOs;
using SXICE.Core.Models.Search;
using SXICE.IRepository;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace SXICE.Repository
{
    /// <summary>
    /// 医生仓储
    /// </summary>
    public class DoctorRepository : DBContext, IDoctorRepository
    {
        private readonly ILog log;

        public DoctorRepository(ILog log)
        {
            this.log = log;
        }

        public ReturnResult<Doctor> GetOneById(int id)
        {
            try
            {
                var customer = DoctorDB.GetById(id);
                if (null == customer)
                    return RepositoryErrors.NO_DOCTOR.ToResult<Doctor>("医生不存在");
                return customer.ToResult();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return ex.ToExceptionResult<Doctor>();
            }
        }

        public ReturnResult<bool> InsertOne(Doctor obj)
        {
            try
            {
                return DoctorDB.Insert(obj).ToResult();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return ex.ToExceptionResult<bool>();
            }
        }

        public ReturnResult<bool> UpdateObject(Doctor obj)
        {
            try
            {
                return (DoctorDB.AsUpdateable(obj).UpdateColumns(nameof(obj.RealName), obj.Password.IsNullOrEmpty() ? null : nameof(obj.Password)).ExecuteCommand() > 0).ToResult();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return ex.ToExceptionResult<bool>();
            }
        }

        public ReturnResult<Doctor> GetOneByName(string name)
        {
            try
            {
                var user = DoctorDB.GetSingle(u => u.UserName == name);
                if (null == user)
                    return RepositoryErrors.NO_DOCTOR.ToResult<Doctor>("医生不存在");
                return user.ToResult();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return ex.ToExceptionResult<Doctor>();
            }

        }

        public ReturnPageListResult<Doctor> Search(PageListQuery<RecordSearchModel> pageListQuery)
        {
            try
            {
                var condition = pageListQuery.Condition ?? new RecordSearchModel();
                var total = 0;
                return Db.Queryable<Doctor>().WhereIF(!condition.Name.IsNullOrEmpty(), t => t.UserName.Contains(pageListQuery.Condition.Name))
                     .OrderBy(t => t.Id, OrderByType.Desc).ToPageList(pageListQuery.PageIndex, pageListQuery.PageSize, ref total).ToResult(total);
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return ex.ToExceptionPageResult<Doctor>();
            }
        }

        public ReturnResult ExistsByRoleId(int roleId)
        {
            try
            {
                return DoctorDB.IsAny(c => c.RoleId == roleId) ? true.ToResult() : RepositoryErrors.NO_DOCTOR.ToResult("已不存在此角色的用户");
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return ex.ToExceptionResult();
            }
        }

        public ReturnResult<bool> UpdateRole(Doctor doctor)
        {
            try
            {
                return (DoctorDB.AsUpdateable(doctor).UpdateColumns(c => new { c.RoleId, c.RoleName }).ExecuteCommand() > 0).ToResult();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return ex.ToExceptionResult<bool>();
            }
        }

        public ReturnResult<bool> DeleteDoctor(int id, bool state)
        {
            try
            {
                return DoctorDB.Update(c => new Doctor { Enabled = state }, c => c.Id == id).ToResult();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return ex.ToExceptionResult<bool>();
            }
        }

        public ReturnResult<bool> UpdatePassword(Doctor doctor)
        {
            try
            {
                return DoctorDB.Update(c => new Doctor { Password = doctor.Password }, c => c.Id == doctor.Id).ToResult();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return ex.ToExceptionResult<bool>();
            }
        }

        public ReturnResult IsEnabled(int id)
        {
            try
            {
                return DoctorDB.AsQueryable().Where(c => c.Id == id).Select(c => c.Enabled).First() ? true.ToResult() : RepositoryErrors.DOCTOR_DISABLED.ToResult("此账号已被禁用");
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return ex.ToExceptionResult();
            }
        }
    }
}
